// Parses the input string for the numerators and denominators
function compile(prog, numArr, denArr) {
    let regex = /\s*(\d*)\s*\/\s*(\d*)\s*(.*)/m;
    let result;
    while (result = regex.exec(prog)) {
        numArr.push(result[1]);      // (\d*)
        denArr.push(result[2]);      // (\d*)
        prog = result[3];            // (.*)
        // console.log('complie',result);
    }
    return [numArr, denArr];
}

// Outputs the result of the compile stage
function dump(numArr, denArr) {
    let output = "";
    for (let i in numArr) {
        output += `${numArr[i]}/${denArr[i]} `;
    }
    return `${output}<br>`;
}

// Step
function step(val, numArr, denArr) {
    let i = 0;
    while (i < denArr.length && val % denArr[i] != 0) i++;
    // console.log(` * ${numArr[i]} / ${denArr[i]} `);
    return numArr[i] * val / denArr[i];
}

// Executes Fractran
function exec(val, i, limit, numArr, denArr) {
    let output = "";
    while (val && i < limit) {
        console.log(`${i}: ${val}`);
        val = step(val, numArr, denArr);
        i++;
    }
    return output;
}

let num, dem;

// PRIMEGAME:
console.log('Play PRIMEGAME');
[num, den] = compile("17/91 78/85 19/51 23/38 29/33 77/29 95/23 77/19 1/17 11/13 13/11 15/2 1/7 55/1", [], []);
/*
    is started at 2,After 2, this sequence contains the following powers of 2:
    N: 19     69     280     707       
    2^2=4, 2^3=8, 2^5=32, 2^7=128, 2^11=2048, 2^13=8192, 2^17=131072, 2^19=524288
    which are the prime powers of 2.
*/
exec(2, 0, 100, num, den);


// PIGAME:
console.log('Play PIGAME');
[num, den] = compile(`365/46 29/161 79/575 679/451 3159/413 83/407 473/371 638/355 
    434/335 89/235 17/209 79/122 31/183 41/115 517/89 111/83 305/79 23/73
    73/71 61/67 37/61 19/59 89/57 41/53 833/47 53/43 86/41 13/38 23/37 67/31
    71/29 83/19 475/17 59/13 41/291 1/7 1/11 1/1024 1/97 89/1 `, [], []);
/*
    is started at 2^n,the next power of 2 to appear is 2^π(n), where for 
    n  = 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
    π(n) = 3 1 4 1 5 9 2 6 5 3  5  8  9  7  9  3  2  3  8  4  6
    For an arbitrary natural number n, π(n) is the nth digit after the 
    point in the decimal expansion of the number π.
*/
exec(1, 0, 100, num, den);

/*
Define l e{n) = m if POLYGAME: 
    583/559 629/55 437/1 82/527 615/517 37/329 1/129 1/115 53/86 43/53 23/47 341/46 
    41/43 47/41 29/37 37/31 37/31 299/29 47/23 161/15 527/19 159/7 1/17 1/13 1/3
    when started at stops at c2^(2^n), stops at 2^(2^m),
    and otherwise leave fc(n) undefined. 
    Then every computable function appears among f0,f1,f2,... .
*/
console.log('Play POLYGAME');
[num, den] = compile(`583/559 629/55 437/1 82/527 615/517 37/329 1/129 1/115 53/86
    43/53 23/47 341/46 41/43 47/41 29/37 37/31 37/31 299/29 47/23 161/15 527/19
    159/7 1/17 1/13 1/3` , [], []);

